package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.clouddata.oms.util.DateUtils;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.web.query.QueryEngine;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description: 记录更新日志服务
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/3/28
 * Time: 9:45
 */
@Component
public class LogStageDao {

    /**
     * 新增日志主表
     */
    public static void insertTime(Long id, String startDate, String endDate, int count,String topic) {
        PropertiesConf pconf = (PropertiesConf) ApplicationContextHandle.getBean("propertiesConf");

        String company = pconf.getProperty("ons.comp","test");
        String sql = "insert into t_loginfor (Id,CompanyNmae,StartTime,EndTime,Count,TopicName) values(?,?,?,?,?,?)";
        int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, company, startDate, endDate, count, topic});
    }

    /**
     * 新增日志主表
     */
    public static void updateTime(Long id, int count) {
        String sql = "update t_loginfor set Count = ? where id = ?";
        int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{count,id});
    }

    /**
     * 新增日志子表
     */
    public static void insertLoginformq(String msgId, int msgNum, String errMsg, String topic, String tag, Long loginforId) {
        String sql = "insert into t_loginformq (msgId,msgNum,errMsg,topic,tag,loginforId) values(?,?,?,?,?,?)";
        int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{msgId,msgNum,errMsg,topic,tag,loginforId});
    }

    /**
     * 新增日志子表
     */
    public static void updateLoginformq(JSONObject jsonObject) {

        jsonObject.getJSONArray("");
        // 获取 jsonobject 的数据
        String msgid = jsonObject.getString("msgid");
        String sql = "update t_loginformq set pstatus = 2 where msgid = ?";
        int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{msgid});
    }

    /**
     * 线上错误日志
     */
    @Transactional
    public int addServerErrLog( String type ,String tablename, String topic, String tag, String errmsg, String status,
                                String protime, String createtime) {
        String sql = "insert into server_errlog (type,tablename,topic,tag,errmsg,status,protime,createtime) values (?,?,?,?,?,?,?,?)";
        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{ type,tablename, topic, tag,  errmsg, status, protime,  createtime});

    }

    public int addServerErrLog( String topic, String tag, String errmsg, String server_ip){
        String sql = "insert into server_errlog (type,topic,tag,errmsg,status,protime,createtime,server_ip) values (?,?,?,?,?,?,?,?)";
        String createtime = DateUtils.getStrNowDate();
        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{ "server_bus", topic, tag,  errmsg, "1", createtime,  createtime,server_ip});
    }

    /*public int addServerErrLog( String type ,String topic, String tag, String errmsg){
        String sql = "insert into server_errlog (type,topic,tag,errmsg,status,protime,createtime) values (?,?,?,?,?,?,?)";
        String createtime = DateUtils.getStrNowDate();
        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{ type, topic, tag,  errmsg, "1", createtime,  createtime});
    }*/

}
